package comment

import (
	"maki/model"
	"maki/utils"
	"net/http"

	"github.com/labstack/echo"
)

// AddComment add comment to article
func AddComment(ctx echo.Context) error {
	var obj model.Comment
	if err := utils.BindAndValidate(ctx, &obj); err != nil {
		return err
	}
	var article model.Article
	if model.DB.Find(&article, obj.ArticleID).RecordNotFound() {
		return utils.ErrRecordNotFound
	}
	user, _ := model.GetUser(ctx)
	obj.UserID = user.ID
	if status := model.DB.Create(&obj); status.Error != nil {
		return status.Error
	}
	return ctx.JSON(http.StatusOK, utils.NewResult("success"))
}
